package yx.action;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.log4j.Logger;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.service.ClassAddrbookServiceInt;
import yx.vo.AddrbookVO;
import yx.vo.JsonVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "classAddrbookAction")
public class ClassAddrbookAction extends BaseAction implements ModelDriven<AddrbookVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ClassAddrbookAction.class);

	private AddrbookVO addrbook = new AddrbookVO();

	@Override
	public AddrbookVO getModel() {
		return addrbook;
	}

	private ClassAddrbookServiceInt classAddrbookService;

	public ClassAddrbookServiceInt getClassAddrbookService() {
		return classAddrbookService;
	}

	@Autowired
	public void setClassAddrbookService(ClassAddrbookServiceInt classAddrbookService) {
		this.classAddrbookService = classAddrbookService;
	}

	public void queryAddrbook() {
		super.writeJson(classAddrbookService.datagrid(addrbook));
	}

	public void syncAddrbook() {
		JsonVO j = new JsonVO();
		try {
			classAddrbookService.sync();
			j.setSuccess(true);
			j.setMsg("同步成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}
	
	// TODO:将通讯录导出到指定目录（将数据库中的数据导出为Excel文件），未完成
	public void downloadAddrbook() {
		OutputStream os = null;
		byte[] bytes = null;
		try {
			XSSFWorkbook wb = new XSSFWorkbook();
			XSSFSheet sheet = wb.createSheet("班级通讯录");
			XSSFRow row = sheet.createRow(1);
			for(int i=0;i<5;i++){
				row.createCell(i).setCellValue("10");
				row.createCell(i).setCellValue("11");
				row.createCell(i).setCellValue("12");
				row.createCell(i).setCellValue("13");
				row.createCell(i).setCellValue("14");
			}
			
//			String downloadPath = ServletActionContext.getServletContext().getRealPath("/") + "template\\StudentImport.xlsx";
//			bytes = classAddrbookService.getFileBytes(downloadPath);
//			ServletActionContext.getResponse().reset();
//			ServletActionContext.getResponse().setCharacterEncoding("utf-8");
//			ServletActionContext.getResponse().setHeader("Content-Type", "application/octet-stream");
//			ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment; filename=StudentImport.xlsx");
//			os = ServletActionContext.getResponse().getOutputStream();
//			if (bytes != null) {
//				os.write(bytes);
//				os.flush();
//				os.close();
//			}
//			logger.info("文档下载成功！");
		} catch (Exception e) {
			logger.error("文档下载失败！");
		} finally {
			try {
				if (bytes != null) {
					os.write(bytes);
					os.flush();
				}
				os.close();
			} catch (IOException e) {
				logger.error("流文件关闭异常！");
			}
		}
	}

}
